<?php

namespace app\admin\command;

use app\admin\model\CompanyOrderModel;
use app\admin\model\GoodsOrderModel;
use app\admin\model\RechargeRecord;
use app\admin\model\StatisticsModel;
use app\admin\model\VipOrderModel;
use app\admin\service\CommandService;
use app\api\model\SpecialOrder;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\Db;
use function form\e;

/***
 * 数据统计 20分钟一次
 */
class Statistics extends Command
{

    protected function configure()
    {
        // 指令配置
        $this->setName('Statistics');
        $this->addArgument("date", Argument::OPTIONAL, "date")
            ->setHelp("php think Statistics [date]");
        // 设置参数
//        $this->setDescription('数据统计')->setHelp("php think Statistics");
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln('开始执行');
        $date = $input->getArgument("date") ?? date('Y-m-d');
        $today = strtotime($date);
        $map[] = ['status', '=', 1];
        $map[] = ['pay_time', 'between', [$today, strtotime(date('Y-m-d 23:59:59', $today))]];

        $res['specArr'] = $this->getSpecReload($map);

        $res['packageArr'] = $this->getPackageReload($map);

        $res['goodsArr'] = $this->getGoodsReload($today);

        $res['vipArr'] = $this->getVipReload($map);

        $data['date'] = $date;

        $data['total_amount'] = array_sum(array_column($res,'recharge_amount'));
        $data['total_nums'] = array_sum(array_column($res,'recharge_count'));
        $data['total_user'] = array_sum(array_column($res,'recharge_user'));

        $data['spec_amount'] = $res['specArr']['recharge_amount'];
        $data['spec_nums'] = $res['specArr']['recharge_count'];
        $data['spec_user'] = $res['specArr']['recharge_user'];


        $data['package_amount'] = $res['packageArr']['recharge_amount'];
        $data['package_nums'] = $res['packageArr']['recharge_count'];
        $data['package_user'] = $res['packageArr']['recharge_user'];

        $data['goods_amount'] = $res['goodsArr']['recharge_amount'];
        $data['goods_nums'] = $res['goodsArr']['recharge_count'];
        $data['goods_user'] = $res['goodsArr']['recharge_user'];


        $data['vip_amount'] = $res['vipArr']['recharge_amount'];
        $data['vip_nums'] = $res['vipArr']['recharge_count'];
        $data['vip_user'] = $res['vipArr']['recharge_user'];


        $statistics = (new StatisticsModel())->where(['date' => $date])->find();
        if(!$statistics){
            (new StatisticsModel())->insert($data);
            $output->writeln('添加成功');
        }else{
            (new StatisticsModel())->where(['date' => $date])->update($data);
            $output->writeln('更新成功');
        }

        $output->writeln('执行结束');
    }


    public function getVipReload($map)
    {
        //查询总充值
        $recharge_total = (new VipOrderModel())
            ->where($map)->field('count(id) as recharge_count,SUM(pay_amount) as pay_amount')
            ->find();

        //查询充值人数
        $recharge_user = (new VipOrderModel())
            ->where($map)->group('member_id')
            ->count();

        $data['recharge_user'] = $recharge_user ?? 0;
        $data['recharge_amount'] = $recharge_total['pay_amount'] ?? 0;
        $data['recharge_count'] = $recharge_total['recharge_count'] ?? 0;

        return $data;
    }


    public function getPackageReload($map)
    {
        //查询总充值
        $recharge_total = (new CompanyOrderModel())
            ->where($map)->field('count(id) as recharge_count,SUM(pay_amount) as pay_amount')
            ->find();
        //查询充值人数
        $recharge_user = (new CompanyOrderModel())
            ->where($map)->group('member_id')
            ->count();

        $data['recharge_user'] = $recharge_user ?? 0;
        $data['recharge_amount'] = $recharge_total['pay_amount'] ?? 0;
        $data['recharge_count'] = $recharge_total['recharge_count'] ?? 0;

        return $data;
    }


    public function getGoodsReload($today)
    {
        $map[] = ['status', 'in', [1,2,3]];
        $map[] = ['create_time', 'between', [$today, strtotime(date('Y-m-d 23:59:59', $today))]];
        //查询总充值
        $recharge_total = (new GoodsOrderModel())
            ->where($map)->field('count(id) as recharge_count,SUM(pay_amount) as pay_amount')
            ->find();
        //查询充值人数
        $recharge_user = (new GoodsOrderModel())
            ->where($map)->group('member_id')
            ->count();

        $data['recharge_user'] = $recharge_user ?? 0;
        $data['recharge_amount'] = $recharge_total['pay_amount'] ?? 0;
        $data['recharge_count'] = $recharge_total['recharge_count'] ?? 0;

        return $data;
    }



    public function getSpecReload($map)
    {
        //查询总充值
        $recharge_total = (new SpecialOrder())
            ->where($map)->field('count(id) as recharge_count,SUM(pay_amount) as pay_amount')
            ->find();

        //查询充值人数
        $recharge_user = (new SpecialOrder())
            ->where($map)->group('member_id')
            ->count();

        $data['recharge_user'] = $recharge_user ?? 0;
        $data['recharge_amount'] = $recharge_total['pay_amount'] ?? 0;
        $data['recharge_count'] = $recharge_total['recharge_count'] ?? 0;

        return $data;
    }
}